package osrythm;
/*
 * Composition.java
 *
 * Created on April 14, 2007, 4:20 PM
 * @author Fergus Earnshaw
 */

public class Composition {
    
   
    //Variables
    
    private int iTickState;
    private int iTempo;
    private int iCurrentElement;
    private int iTickStateLast;
    
    private java.util.ArrayList alElements;

    //constants
    
    //Number of Beats in a bar
    private static int iBarCount = 32;

    //notes
    private static Element noteThirtySecond = new ElementNote32nd();
    private static Element noteSixteenth = new ElementNote16th();
    private static Element noteEighth = new ElementNote8th();    
    private static Element noteQuarter = new ElementNoteQuarter();
    private static Element noteHalf = new ElementNoteHalf();
    private static Element noteWhole = new ElementNoteWhole();
    
    //rests
    private static Element restThirtySecond = new ElementRest32nd();
    private static Element restSixteenth = new ElementRest16th();
    private static Element restEighth = new ElementRest8th();    
    private static Element restQuarter = new ElementRestQuarter();
    private static Element restHalf = new ElementRestHalf();
    private static Element restWhole = new ElementRestWhole();    
        
    //functions
       
    public Composition(){
        this.iTickState = 0;
        this.iTempo = 100;
        this.iCurrentElement =0;
        this.iTickStateLast =2;
        
        clearAllTicks();
    }
    
    
    public boolean deleteLastTick(){        
        
        if(this.alElements.size() == 0)
        {
            return false;
        }
        
        Object oO = this.alElements.remove(this.alElements.size()-1);
        
        return true;
    }
    
    public boolean clearAllTicks(){
        
        this.alElements = new java.util.ArrayList();
        return true;
    }
    
    public void incTempo(){
        if(this.iTempo < 200)   {   this.iTempo++;    }        
    }
    
    public void decTempo(){
        if(this.iTempo > 1)     {   this.iTempo--;    }
    }
    
    public int getTempo(){
        return iTempo;
    }
    
    public String getTime(){
        String strTime;
        strTime = "Lentissimo";
        
        if (iTempo > 168)
        {
            return "Presto";
        }
        
        if (iTempo > 120)
        {
            return "Allegro";
        }
        
        if (iTempo > 90)
        {
            return "Moderato";
        }
        
        if (iTempo > 70)
        {
            return "Adagietto";
        }
        
        if (iTempo > 40)
        {
            return "Larghissimo";
        }
        
        return strTime;
    }
            
    public String getTickStr1(){
        String strTick1;
        strTick1 = "";
        
        int iSize = alElements.size();
        if(iSize > 16)
        {
            iSize = 16;
        }
        for (int i = 0; i < iSize; i++)
        {
            strTick1 = strTick1 +((Element)alElements.get(i)).getSymbol() + " ";   
        }        
        
        return strTick1;        
    }
    
    public String getTickStr2(){
        String strTick2;
        strTick2 = "";
        
        int iSize = alElements.size();
        if(iSize > 16)
        {
            if(iSize > 32)
            {
                iSize = 32;
            }
        
            for (int i = 16; i < iSize; i++)
            {
                strTick2 = strTick2 +((Element)alElements.get(i)).getSymbol() + " ";   
            }        
        }
    
        
        return strTick2;
    }
    
    
    public String getTickState(){
        return "/images/pen_st_" + this.iTickState + ".gif";
    }

    public int countMeasure(){
        int iCount=0;
        for(int i = 0; i < this.alElements.size(); i++)
        {
            iCount+=((Element)this.alElements.get(i)).getCount();
        }
        System.out.println(iCount);
        return iCount;
    }
    
    private boolean addElement(Element tmpEl) {
        if(countMeasure()+ tmpEl.getCount() > this.iBarCount)
        {            
            return false;
        }
       
        alElements.add(tmpEl);
        
        return true;
    }
    public boolean addNoteWhole(){
        
        return (addElement(noteWhole));        
    }

    public boolean addNoteHalf(){
               
        return (addElement(noteHalf));
    }

    public boolean addNoteQuarter(){
        return (addElement(noteQuarter));
    }

    public boolean addNote8th(){
        return (addElement(noteEighth));
    }

    public boolean addNote16th(){
        return (addElement(noteSixteenth));
    }

    public boolean addNote32nd(){
        return (addElement(noteThirtySecond));
    }
    
     public boolean addRestWhole(){
        return (addElement(restWhole));
    }

    public boolean addRestHalf(){
        return (addElement(restHalf));
    }

    public boolean addRestQuarter(){
        return (addElement(restQuarter));
    }

    public boolean addRest8th(){
        return (addElement(restEighth));
    }

    public boolean addRest16th(){
        return (addElement(restSixteenth));
    }

    public boolean addRest32nd(){
        return (addElement(restThirtySecond));
    }
    
    public int getElementCount() {
        return alElements.size();
    }
    
    public int getCount(int iTmp){
        return ((Element)alElements.get(iTmp)).getCount();
    }
 
    public void changeState(){
        if(iTickState == 0)
        {
            if(iTickStateLast == 1)
            {
                iTickState = 2;                
            }
            else if(iTickStateLast == 2)
            {
                iTickState = 1;
            }
        }
        
        else if(iTickState == 1)
        {
            iTickStateLast = 1;
            iTickState = 0;
        }
        else if(iTickState == 2)
        {
            iTickStateLast = 2;
            iTickState = 0;            
        }

    }
    
    public String getTick(int i){
        
        int iTmp;
        iTmp = i;
        if (iTmp > this.alElements.size()-1)
        {
            iTmp = 0;
        }
        if(((Element)this.alElements.get(iTmp)).playTick())
        {
            
            if (iTickState == 1)
            {
                return "Tick";
            }
            if (iTickState == 2)
            {
                return "Tock";
            }
        }
        
        return "";
    }
    
    public void startComp()
    {
        this.iTickStateLast = 2;
        this.iTickState =0;
    }

    public void stopComp()
    {
        this.iTickStateLast = 2;
        this.iTickState = 0;
    }


}
